<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of commandes_m
 *
 * @author Gidaff_01
 */
class Commandes_m extends Model {

    //put your code here
    function Commandes_m() {
        parent::Model();
        $this->load->database();
    }

    function get_all_commandes($per_page, $offset) {
        $this->db->select('
            zoo_commande.id,
            lehu_profiles.display_name,
            zoo_commande.dat,
            zoo_commande.status,
            zoo_commande.montant,
            zoo_commande.livre,
            zoo_method_paiement.nom,
            zoo_commande.adr,
            lehu_profiles.user_id
        ');
        $this->db->from('lehu_profiles ,zoo_commande ,zoo_method_paiement');
        $this->db->where('lehu_profiles.user_id = zoo_commande.idclient');
        $this->db->where('zoo_commande.method = zoo_method_paiement.id');
        $this->db->order_by('zoo_commande.id', 'DESC');
        $this->db->limit($per_page, $offset);
        $query = $this->db->get();
        return $query->result_array();
    }

    function count_all_commandes() {
        $this->db->select('
            zoo_commande.id,
            lehu_profiles.display_name,
            zoo_commande.dat,
            zoo_commande.montant,
            zoo_commande.livre,
            zoo_method_paiement.nom,
            zoo_commande.adr,
            lehu_profiles.user_id
        ');
        $this->db->from('lehu_profiles ,zoo_commande ,zoo_method_paiement');
        $this->db->where('lehu_profiles.user_id = zoo_commande.idclient');
        $this->db->where('zoo_commande.method = zoo_method_paiement.id');
        $this->db->order_by('zoo_commande.id', 'DESC');
        $query = $this->db->get();
        return count($query->result_array());
    }

    function delivered($commandes_id) {
        return $this->db->update('zoo_commande', array('livre' => now()), array('id' => $commandes_id));
    }

    function delete_commandes($commande_id) {
        $this->db->delete('zoo_detail_commande', array('idcommande' => $commande_id));
        return $this->db->delete('zoo_commande', array('id' => $commande_id));
    }

    function get_commande_by_id($commandes_id) {
        $this->db->select('
            zoo_commande.id,
            lehu_profiles.display_name,
            lehu_profiles.user_id,
            lehu_profiles.first_name,
            lehu_profiles.last_name,
            lehu_profiles.phone,
            lehu_profiles.mobile,
            lehu_profiles.address,
            lehu_users.email,
            zoo_commande.dat,
            zoo_commande.montant,
            zoo_commande.livre,
            zoo_method_paiement.nom,
            zoo_commande.adr,
            zoo_commande.status,
            zoo_commande.id_livraison
        ');
        $this->db->from('lehu_profiles ,zoo_commande ,zoo_method_paiement,zoo_detail_commande,lehu_users');
        $this->db->where('lehu_profiles.user_id = zoo_commande.idclient');
        $this->db->where('lehu_users.id = lehu_profiles.user_id');
        $this->db->where('zoo_commande.method = zoo_method_paiement.id');
        $this->db->where('zoo_detail_commande.idcommande = zoo_commande.id');
        $this->db->where('zoo_detail_commande.idcommande', $commandes_id);
        $query = $this->db->get();
        return $query;
    }

    function get_product_by_commande_id($commandes_id) {
        $this->db->select('
            zoo_produit.nomfr,
            zoo_produit.prix,
            zoo_detail_commande.quantite
        ');
        $this->db->from('zoo_produit ,zoo_detail_commande');
        $this->db->where('zoo_produit.id = zoo_detail_commande.idproduit');
        $this->db->where('zoo_detail_commande.idcommande', $commandes_id);
        $query = $this->db->get();
        return $query->result_array();
    }

    function get_transport_id($commandes_id) {
        $this->db->select('zoo_transporteur.id');
        $this->db->from('zoo_tarif ,zoo_commande,zoo_transporteur');
        $this->db->where('zoo_tarif.id = zoo_commande.id_livraison');
        $this->db->where('zoo_tarif.id_transporteur = zoo_transporteur.id');
        $this->db->where('zoo_commande.id', $commandes_id);
        $query = $this->db->get();
        return $query;
    }

    function get_transport() {
        $this->db->select();
        $this->db->from('zoo_transporteur');
        $query = $this->db->get();
        return $query->result_array();
    }

    function get_commandes_by_livre($livre, $per_page, $offset) {
        if ($livre == 'all') {
            return $this->get_all_commandes($per_page, $offset);
        } else if ($livre == 'non') {
            $this->db->select('
                zoo_commande.id,
                lehu_profiles.display_name,
                zoo_commande.dat,
                zoo_commande.montant,
                zoo_commande.livre,
                zoo_method_paiement.nom,
                zoo_commande.adr,
                lehu_profiles.user_id
            ');
            $this->db->from('lehu_profiles ,zoo_commande ,zoo_method_paiement');
            $this->db->where('lehu_profiles.user_id = zoo_commande.idclient');
            $this->db->where('zoo_commande.method = zoo_method_paiement.id');
            $this->db->where('zoo_commande.livre', NULL);
            $this->db->order_by('zoo_commande.id', 'ASC');
            $this->db->limit($per_page, $offset);
            $query = $this->db->get();
            return $query->result_array();
        } else {
            $this->db->select('
                zoo_commande.id,
                lehu_profiles.display_name,
                zoo_commande.dat,
                zoo_commande.montant,
                zoo_commande.livre,
                zoo_method_paiement.nom,
                zoo_commande.adr,
                lehu_profiles.user_id
            ');
            $this->db->from('lehu_profiles ,zoo_commande ,zoo_method_paiement');
            $this->db->where('lehu_profiles.user_id = zoo_commande.idclient');
            $this->db->where('zoo_commande.method = zoo_method_paiement.id');
            $this->db->where('zoo_commande.livre IS NOT NULL');
            $this->db->order_by('zoo_commande.id', 'ASC');
            $this->db->limit($per_page, $offset);
            $query = $this->db->get();
            return $query->result_array();
        }
    }

    function count_commandes_by_livre($livre) {
        if ($livre == 'all') {
            return $this->count_all_commandes();
        } else if ($livre == 'non') {
            $this->db->select('
                zoo_commande.id,
                lehu_profiles.display_name,
                zoo_commande.dat,
                zoo_commande.montant,
                zoo_commande.livre,
                zoo_method_paiement.nom,
                zoo_commande.adr,
                lehu_profiles.user_id
            ');
            $this->db->from('lehu_profiles ,zoo_commande ,zoo_method_paiement');
            $this->db->where('lehu_profiles.user_id = zoo_commande.idclient');
            $this->db->where('zoo_commande.method = zoo_method_paiement.id');
            $this->db->where('zoo_commande.livre', NULL);
            $this->db->order_by('zoo_commande.id', 'ASC');
            $query = $this->db->get();
            return count($query->result_array());
        } else {
            $this->db->select('
                zoo_commande.id,
                lehu_profiles.display_name,
                zoo_commande.dat,
                zoo_commande.montant,
                zoo_commande.livre,
                zoo_method_paiement.nom,
                zoo_commande.adr,
                lehu_profiles.user_id
            ');
            $this->db->from('lehu_profiles ,zoo_commande ,zoo_method_paiement');
            $this->db->where('lehu_profiles.user_id = zoo_commande.idclient');
            $this->db->where('zoo_commande.method = zoo_method_paiement.id');
            $this->db->where('zoo_commande.livre IS NOT NULL');
            $this->db->order_by('zoo_commande.id', 'ASC');
            $query = $this->db->get();
            return count($query->result_array());
        }
    }

    function get_user_by_id($user_id) {
        $this->db->select('lehu_users.email,lehu_profiles.display_name');
        $this->db->from('lehu_users,lehu_profiles');
        $this->db->where('lehu_users.id = lehu_profiles.user_id');
        $this->db->where('lehu_users.id', $user_id);
        return $this->db->get();
    }

}

?>
